package com.glela.serial.service.impl;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.glela.cache.model.RedisKey;
import com.glela.cache.redis.RedisUtil;
import com.glela.common.constant.BaseConstants;
import com.glela.common.model.ProductTag;
import com.glela.common.util.DateUtil;
import com.glela.goods.mapper.GoodsMapper;
import com.glela.goods.model.GoodsPrice;
import com.glela.goods.model.StoreGoods;
import com.glela.goods.model.vo.goods.GoodSerialVo;
import com.glela.goods.service.GoodsService;
import com.glela.goods.service.InventoryService;
import com.glela.goods.service.StoreGoodsService;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.User;
import com.glela.platform.model.UserInfo;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.model.response.ResponseUser;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.service.UserService;
import com.glela.serial.mapper.SerialContentMapper;
import com.glela.serial.mapper.SerialGoodsMapper;
import com.glela.serial.model.SerialContent;
import com.glela.serial.model.request.RequestSerial;
import com.glela.serial.model.response.ResponseSerialContent;
import com.glela.serial.model.response.hometitle.ResponseHomeTitleInfo;
import com.glela.serial.model.response.hometitle.ResponseHomeTitleInfoGoodsVo;
import com.glela.serial.service.SerialGoodService;
@Service("serialGoodService")
public class SerialGoodServiceImpl implements SerialGoodService {
	//	private static Logger logger = LoggerFactory.getLogger(SerialGoodServiceImpl.class);
	
	@Autowired
	private SerialGoodsMapper serialGoodsMapper ;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private InventoryService inventoryService;
	@Autowired
	private StoreGoodsService storeGoodsService;
	@Autowired
	private SerialContentMapper	serialContentMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private UserService userService;
	@Autowired
	private BaseRestrictionsService  baseRestrictionsService;
	
	@Autowired
	RedisUtil redisUtil ;
	
	
	@Override
	public ResponseHomeTitleInfo queryAllProductIdsBySerialId(RequestSerial request) {
//		System.out.println(System.currentTimeMillis());
		//获取用户信息
//		User user = null ;
//		if (null != request.getUserId() && request.getUserId() > 0) {
//			user = userMapper.selectByPrimaryKey(request.getUserId());
//		}
		// 店主用户ID  默认是校妆姐姐店铺的店主ID
		long storeUserId = this.baseRestrictionsService.getBaseRestrictionsForCache("xz_jj").getExperience();
		/*ResponseUser _login_user = null;//登录用户
		if (null != request.getUserId() && request.getUserId() > 0) {
			_login_user = this.userService.selectUserRoleById(request.getUserId());
//			int roleId = 0;
			if (_login_user != null && StringUtils.isNotEmpty(_login_user.getRecommendFrom())){
//				roleId = user.getRoleId();
				//登录人对应的店主身份ID
				String recommendFrom = _login_user.getRecommendFrom();
				UserInfo userInfo = this.userService.selectUserInfoByPersionId(recommendFrom);//推荐人身份
				if(userInfo!=null){
					storeUserId = userInfo.getUserId();		
				}
			}
		}
		User _store_user = this.userMapper.selectByPrimaryKey(storeUserId);*/
		User _login_user = null;//登录用户
		if (null != request.getUserId() && request.getUserId() > 0) {
			_login_user = this.userService.getUserById(request.getUserId());
			if (_login_user != null && StringUtils.isNotEmpty(_login_user.getRecommendFrom())){
				User userInfo = this.userService.getUserByPersionId(_login_user.getRecommendFrom());//推荐人身份
				if(userInfo!=null){
					storeUserId = userInfo.getId();
				}
			}
		}
		User _store_user = this.userService.getUserById(storeUserId);
		ResponseHomeTitleInfo returnSerial = new ResponseHomeTitleInfo();
		returnSerial.setSerialId(request.getSerialId());
		List<GoodSerialVo> list = serialGoodsMapper.selectGoodsVoBySerialIdPage(request.getSerialId(),request.getStartIndex(),request.getPageSize());
		List<ResponseHomeTitleInfoGoodsVo>  goodList = new ArrayList<ResponseHomeTitleInfoGoodsVo>();
		if (null != list && !list.isEmpty()) {
			List<Long> goodsIds = new ArrayList<>();
			for (GoodSerialVo gVo : list) {
				goodsIds.add(gVo.getGoodId());
			}
			Map<Long, GoodsPrice> goodsPriceMap = goodsService.getGoodsViewPriceForGoods(_store_user, goodsIds);
			Map<Long, Long> dynaByGoodsIdMap = inventoryService.findDynaByGoodsId(goodsIds);
			Map<Long, StoreGoods> goodsIdStoreGoodsMap = null;//当前用户上架商品
			if (_login_user == null) {
				goodsIdStoreGoodsMap = new HashMap<>();
			} else {
				goodsIdStoreGoodsMap = storeGoodsService.queryStoreGoods(_login_user.getId(), goodsIds);
			}
			for (GoodSerialVo goods : list) {
//				Map<String, Object> params = new HashMap<>();
//				params.put("product_sn", goods.getGoodId());
//				params.put("current_time", new Date());
				ResponseHomeTitleInfoGoodsVo vo = new ResponseHomeTitleInfoGoodsVo();
				vo.setGoodsId(goods.getGoodId());
				vo.setName(goods.getName());
				vo.setLabelName(goods.getLabelName());
				vo.setSelfSupport(goods.getSelfSupport());
				vo.setInventoryAvlNum(goods.getInventoryNum());
				vo.setNavigateImage(goods.getNavigateImage());
				vo.setOrderNo(goods.getOrderNo());
//				if (null == _login_user) {//未登录
//					vo.setUpOrDown(0);
//				}else{
//					List<Map<String, Object>> sList =  serialGoodsMapper.selectStoreGoodsByUserId(goods.getGoodId(),_login_user.getId());
//					if (sList.isEmpty() || null == list) {//该店主没有上架该商品
//						vo.setUpOrDown(0);
//					}else{
//						vo.setUpOrDown(1);
//					}
//				}
				if (goodsIdStoreGoodsMap.get(goods.getGoodId())==null||goodsIdStoreGoodsMap.get(goods.getGoodId()).getIsDelete()==1) {
					vo.setUpOrDown(0);
				}else{
					vo.setUpOrDown(1);
				}
				//查询动态价格
//				GoodsPrice goodsViewPriceForProduct = goodsService.getGoodsViewPriceForGoods(u, goods.getGoodId());
//				if (goodsViewPriceForProduct != null) {
//					vo.setLowestPrice(goodsViewPriceForProduct.getSalePrice());
//					vo.setMarketPirce(goodsViewPriceForProduct.getMarketPirce());
//					vo.setCommission(goodsViewPriceForProduct.getCommission());
//				}
				//查询实时库存
//				long inventoryAvlNum = 0L;
//				DynamicInventoryVo findDynaByProductId = inventoryService.findDynaByProductId(goodsViewPriceForProduct.getProductId());
//				if (null != findDynaByProductId) {
//					inventoryAvlNum = findDynaByProductId.getInventoryNumAvl();
//				}
//				inventoryAvlNum = inventoryService.findDynaByGoodsId(goods.getGoodId());
//				vo.setInventoryAvlNum(inventoryAvlNum);
				///////////////////批量查询价格和库存////////////////////////////////////////
				if (dynaByGoodsIdMap.containsKey(goods.getGoodId())) {
					if (null != dynaByGoodsIdMap.get(goods.getGoodId())) {
						vo.setInventoryAvlNum(dynaByGoodsIdMap.get(goods.getGoodId()));
					}else {
						vo.setInventoryAvlNum(0);
					}
				}else {
					vo.setInventoryAvlNum(0);
				}
				GoodsPrice price = null;
				if (goodsPriceMap.containsKey(goods.getGoodId())) {
					price = goodsPriceMap.get(goods.getGoodId());
					if (price != null) {
						vo.setLowestPrice(price.getSalePrice());
						vo.setMarketPirce(price.getMarketPirce());
						vo.setCommission(price.getCommission());
					}else {
						vo.setInventoryAvlNum(0);
					}
				}else {
					vo.setInventoryAvlNum(0);
				}					
				//商品标签
				List<ProductTag> ptList = new ArrayList<ProductTag>();
//				ProductTag p = new ProductTag();
//				p.setColor("#FF0000");
//				List<LimitTimeSaleDetail> limitList = limitTimeSaleDetailMapper.selectByGoodsId(params);
//				if (null != limitList && !limitList.isEmpty()) {//特卖中
//					p.setLabel("特卖");
//				}else{
//					if (goods.getSelfSupport() == 0) {
//						p.setLabel("自营");
//					}else{
//						p.setLabel("非自营");
//					}
//				}
				String nowTime = DateUtil.currentStringDate();
				//状态
				Map<String,Object> ltsMap = null;
				if (null != price) {
					ltsMap = this.goodsMapper.selectNearlyLimitTimeSale(goods.getGoodId(),price.getProductId(),nowTime);
				}
//				Map<String,Object> ltsMap = this.goodsMapper.selectNearlyLimitTimeSale(goods.getGoodId(),goodsViewPriceForProduct.getProductId());
				if(ltsMap != null){
					ProductTag p = new ProductTag();
					p.setColor("#ff4b7a");
					try {
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//抢购开始时间
						Date fullBeginTime = sdf.parse(ltsMap.get("fullBeginTime").toString());
						// 系统当前时间
						long nowTimeMillis = System.currentTimeMillis();
						if (fullBeginTime.getTime() > nowTimeMillis) {// 预热中
							p.setLabel("预热");
						} else {
							p.setLabel("特卖");
						}
					} catch (Exception e) {
						
					}
					ptList.add(p);
				}
				//属性
				//去除自营标签----2018-01-15
				/*if (goods.getSelfSupport() == 0) {
					ProductTag pT = new ProductTag();
					pT.setColor("#ff4b7a");
					pT.setLabel("自营");
					ptList.add(pT);
				}*/
//				else if(goods.getSelfSupport() == 1) {
//					pT.setLabel("非自营");
//				}
				vo.setProductTags(ptList);
				goodList.add(vo);
			}
		}
		returnSerial.setGoodsList(goodList);
		if (request.getPageNum() <= 1) {//第一页的时候才返回滚动图
			returnSerial.setSerialContentImages(new LinkedList<ResponseSerialContent>());
			List<SerialContent> contents = serialContentMapper.selectSerialContentListBySerialId(request.getSerialId());
			for (SerialContent sc : contents) {
				ResponseSerialContent rsc = new ResponseSerialContent();
				rsc.setSerialContentId(sc.getId());
				rsc.setSerialId(sc.getSerialId());
				rsc.setImagePath(sc.getImagePath());
				rsc.setJumpStyle(sc.getJumpStyle());
				rsc.setJumpTo(sc.getJumpTo());
				rsc.setImgWidth(sc.getImgWidth());
				rsc.setImgHeight(sc.getImgHeight());
				returnSerial.getSerialContentImages().add(rsc);
			}
		}
//		System.out.println(System.currentTimeMillis());
		return returnSerial;
	}
	
	
	
	public ResponseHomeTitleInfo getAllProductIdsBySerialId(RequestSerial request) {
		long storeUserId = this.baseRestrictionsService.getBaseRestrictionsForCache("xz_jj").getExperience();
		ResponseUser _login_user = null;//登录用户
		if (null != request.getUserId() && request.getUserId() > 0) {
			_login_user = this.userService.selectUserRoleById(request.getUserId());
			if (_login_user != null && StringUtils.isNotEmpty(_login_user.getRecommendFrom())){
//				roleId = user.getRoleId();
				//登录人对应的店主身份ID
				String recommendFrom = _login_user.getRecommendFrom();
				UserInfo userInfo = this.userService.selectUserInfoByPersionId(recommendFrom);//推荐人身份
				if(userInfo!=null){
					storeUserId = userInfo.getUserId();		
				}
			}
		}
		User _store_user = this.userMapper.selectByPrimaryKey(storeUserId);//使用推荐人去计算价格（默认校妆姐姐）
		ResponseHomeTitleInfo returnSerial = new ResponseHomeTitleInfo();
		returnSerial.setSerialId(request.getSerialId());
		List<GoodSerialVo> list = serialGoodsMapper.selectGoodsVoBySerialIdPage(request.getSerialId(),request.getStartIndex(),request.getPageSize());
		List<ResponseHomeTitleInfoGoodsVo>  goodList = new ArrayList<ResponseHomeTitleInfoGoodsVo>();
		if (null != list && !list.isEmpty()) {
			List<Long> goodsIds = new ArrayList<>();
			for (GoodSerialVo gVo : list) {
				goodsIds.add(gVo.getGoodId());
			}
			Map<Long, GoodsPrice> goodsPriceMap = goodsService.getGoodsViewPriceForGoods(_store_user, goodsIds);
			Map<Long, Long> dynaByGoodsIdMap = inventoryService.findDynaByGoodsId(goodsIds);
			Map<Long, StoreGoods> goodsIdStoreGoodsMap = null;//当前用户上架商品
			if (_login_user == null) {
				goodsIdStoreGoodsMap = new HashMap<>();
			} else {
				goodsIdStoreGoodsMap = storeGoodsService.queryStoreGoods(_login_user.getUserId(), goodsIds);
			}
			for (GoodSerialVo goods : list) {
//				Map<String, Object> params = new HashMap<>();
//				params.put("product_sn", goods.getGoodId());
//				params.put("current_time", new Date());
				ResponseHomeTitleInfoGoodsVo vo = new ResponseHomeTitleInfoGoodsVo();
				vo.setGoodsId(goods.getGoodId());
				vo.setName(goods.getName());
				vo.setLabelName(goods.getLabelName());
				vo.setSelfSupport(goods.getSelfSupport());
				vo.setInventoryAvlNum(goods.getInventoryNum());
				vo.setNavigateImage(goods.getNavigateImage());
				vo.setOrderNo(goods.getOrderNo());
//				if (null == _login_user) {//未登录
//					vo.setUpOrDown(0);
//				}else{
//					List<Map<String, Object>> sList =  serialGoodsMapper.selectStoreGoodsByUserId(goods.getGoodId(),_login_user.getId());
//					if (sList.isEmpty() || null == list) {//该店主没有上架该商品
//						vo.setUpOrDown(0);
//					}else{
//						vo.setUpOrDown(1);
//					}
//				}
				if (goodsIdStoreGoodsMap.get(goods.getGoodId())==null||goodsIdStoreGoodsMap.get(goods.getGoodId()).getIsDelete()==1) {
					vo.setUpOrDown(0);
				}else{
					vo.setUpOrDown(1);
				}
				//查询动态价格
//				GoodsPrice goodsViewPriceForProduct = goodsService.getGoodsViewPriceForGoods(u, goods.getGoodId());
//				if (goodsViewPriceForProduct != null) {
//					vo.setLowestPrice(goodsViewPriceForProduct.getSalePrice());
//					vo.setMarketPirce(goodsViewPriceForProduct.getMarketPirce());
//					vo.setCommission(goodsViewPriceForProduct.getCommission());
//				}
				//查询实时库存
//				long inventoryAvlNum = 0L;
//				DynamicInventoryVo findDynaByProductId = inventoryService.findDynaByProductId(goodsViewPriceForProduct.getProductId());
//				if (null != findDynaByProductId) {
//					inventoryAvlNum = findDynaByProductId.getInventoryNumAvl();
//				}
//				inventoryAvlNum = inventoryService.findDynaByGoodsId(goods.getGoodId());
//				vo.setInventoryAvlNum(inventoryAvlNum);
				///////////////////批量查询价格和库存////////////////////////////////////////
				if (dynaByGoodsIdMap.containsKey(goods.getGoodId())) {
					if (null != dynaByGoodsIdMap.get(goods.getGoodId())) {
						vo.setInventoryAvlNum(dynaByGoodsIdMap.get(goods.getGoodId()));
					}else {
						vo.setInventoryAvlNum(0);
					}
				}else {
					vo.setInventoryAvlNum(0);
				}
				GoodsPrice price = null;
				if (goodsPriceMap.containsKey(goods.getGoodId())) {
					price = goodsPriceMap.get(goods.getGoodId());
					if (price != null) {
						vo.setLowestPrice(price.getSalePrice());
						vo.setMarketPirce(price.getMarketPirce());
						vo.setCommission(price.getCommission());
					}else {
						vo.setInventoryAvlNum(0);
					}
				}else {
					vo.setInventoryAvlNum(0);
				}					
				//商品标签
				List<ProductTag> ptList = new ArrayList<ProductTag>();
//				ProductTag p = new ProductTag();
//				p.setColor("#FF0000");
//				List<LimitTimeSaleDetail> limitList = limitTimeSaleDetailMapper.selectByGoodsId(params);
//				if (null != limitList && !limitList.isEmpty()) {//特卖中
//					p.setLabel("特卖");
//				}else{
//					if (goods.getSelfSupport() == 0) {
//						p.setLabel("自营");
//					}else{
//						p.setLabel("非自营");
//					}
//				}
				String nowTime = DateUtil.currentStringDate();
				//状态
				Map<String,Object> ltsMap = null;
				if (null != price) {
					ltsMap = this.goodsMapper.selectNearlyLimitTimeSale(goods.getGoodId(),price.getProductId(),nowTime);
				}
//				Map<String,Object> ltsMap = this.goodsMapper.selectNearlyLimitTimeSale(goods.getGoodId(),goodsViewPriceForProduct.getProductId());
				if(ltsMap != null){
					ProductTag p = new ProductTag();
					p.setColor("#ff4b7a");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//抢购开始时间
					try {
						Date fullBeginTime = sdf.parse(ltsMap.get("fullBeginTime").toString());
						// 系统当前时间
						long nowTimeMillis = System.currentTimeMillis();
						if (fullBeginTime.getTime() > nowTimeMillis) {// 预热中
							p.setLabel("预热");
						} else {
							p.setLabel("特卖");
						}
					} catch (Exception e) {
						
					}
					ptList.add(p);
				}
				//属性
				//去除自营标签-----2018-1-15
				/*if (goods.getSelfSupport() == 0) {
					ProductTag pT = new ProductTag();
					pT.setColor("#ff4b7a");
					pT.setLabel("自营");
					ptList.add(pT);
				}*/
//				else if(goods.getSelfSupport() == 1) {
//					pT.setLabel("非自营");
//				}
				vo.setProductTags(ptList);
				goodList.add(vo);
			}
			returnSerial.setHasGoodsNextPage(true);
		}else {
			returnSerial.setHasGoodsNextPage(false);
		}
		returnSerial.setGoodsList(goodList);
		if (request.getPageNum() <= 1) {//第一页的时候才返回滚动图
			returnSerial.setSerialContentImages(new LinkedList<ResponseSerialContent>());
			List<SerialContent> contents = serialContentMapper.selectSerialContentListBySerialId(request.getSerialId());
			for (SerialContent sc : contents) {
				ResponseSerialContent rsc = new ResponseSerialContent();
				rsc.setSerialContentId(sc.getId());
				rsc.setSerialId(sc.getSerialId());
				rsc.setImagePath(sc.getImagePath());
				rsc.setJumpStyle(sc.getJumpStyle());
				rsc.setJumpTo(sc.getJumpTo());
				rsc.setImgWidth(sc.getImgWidth());
				rsc.setImgHeight(sc.getImgHeight());
				returnSerial.getSerialContentImages().add(rsc);
			}
		}
//		System.out.println(System.currentTimeMillis());
//		return new ResponseEntity<>(new ResponseResultModel<>(returnSerial), HttpStatus.OK);
		return returnSerial;
	}
	
	
	
	@Override
	public ResponseEntity<?> queryAllProductIdsBySerialIdForCache(RequestSerial request) {
		request.setPageSize(10);
		int currPageIndex = request.getPageNum();
		String _login_user_store_flag = "STORE"; //登录店铺的标识，用来区分一般店铺和校妆姐姐店铺，默认STORE ： 为一般店铺标识
		long storeUserId = this.baseRestrictionsService.getBaseRestrictionsForCache("xz_jj").getExperience();
		int cache_timeout = this.baseRestrictionsService.getBaseRestrictionsForCache(RedisKey.CACHE_TIME_OUT_KEY).getExperience();
		if(cache_timeout < 1) cache_timeout = RedisKey.DEFUALT_CACHE_TIME_OUT;
		ResponseUser _login_user = null;//登录用户
		if (null != request.getUserId() && request.getUserId() > 0) {
			_login_user = this.userService.selectUserRoleById(request.getUserId());
			if (_login_user != null && StringUtils.isNotEmpty(_login_user.getRecommendFrom())){
				//登录人对应的店主身份ID
				String recommendFrom = _login_user.getRecommendFrom();
				UserInfo userInfo = this.userService.selectUserInfoByPersionId(recommendFrom);//推荐人身份
				if(userInfo!=null){
					if(userInfo.getUserId() == storeUserId) {
						_login_user_store_flag = "XZJJ";
					}else{
						_login_user_store_flag = "STORE";
					}
					
				}else {
					_login_user_store_flag = "XZJJ";
				}
			}
		}else {
			_login_user_store_flag = "XZJJ";
		}
		
		String cacheKey = RedisKey.SERIAL_GOOD_LIST_KEY + _login_user_store_flag + ":" + request.getSerialId() + ":" + currPageIndex;
		
		String object_value = redisUtil.get(cacheKey);
		
		if(StringUtils.isBlank(object_value)) {//之前缓存过期重新进行缓存
			ResponseHomeTitleInfo 	_tempResponseHomeTitleInfo =  null;
			int initPage = 1;
			RequestSerial _requestSerial = new RequestSerial();
			_requestSerial.setPageNum(initPage);
			_requestSerial.setPageSize(request.getPageSize());
			_requestSerial.setUserId(_requestSerial.getUserId() );
			_requestSerial.setSerialId(request.getSerialId());
			
			do{
				_tempResponseHomeTitleInfo =  getAllProductIdsBySerialId(_requestSerial);
				if(_tempResponseHomeTitleInfo != null && (!CollectionUtils.isEmpty(_tempResponseHomeTitleInfo.getGoodsList()))) {
					redisUtil.set(RedisKey.SERIAL_GOOD_LIST_KEY + _login_user_store_flag + ":" + request.getSerialId() + ":" + initPage, JSON.toJSON(_tempResponseHomeTitleInfo).toString() , cache_timeout);
					initPage++;
					_requestSerial.setPageNum(initPage);
				}
			}while(_tempResponseHomeTitleInfo.isHasGoodsNextPage());
			
			
		}
		object_value = redisUtil.get(cacheKey);
		ResponseHomeTitleInfo v =  JSON.parseObject(object_value, new TypeReference<ResponseHomeTitleInfo>() {});
		return new ResponseEntity<>(new ResponseResultModel<>(v), HttpStatus.OK);
	} 
	
	/**
	 * 专题页预览
	 */
	@Override
	public ResponseHomeTitleInfo serialGoodsPreview(int serialId, int startIndex, int pageSize) {
		// 店主用户ID  默认是校妆姐姐店铺的店主ID
		long storeUserId = BaseConstants.XZ_SISTER;
		User _store_user = this.userMapper.selectByPrimaryKey(storeUserId);//使用推荐人去计算价格（默认校妆姐姐）
		ResponseHomeTitleInfo returnSerial = new ResponseHomeTitleInfo();
		returnSerial.setSerialId(serialId);
		List<GoodSerialVo> list = serialGoodsMapper.selectGoodsVoBySerialIdPage(serialId,startIndex,pageSize);
		List<ResponseHomeTitleInfoGoodsVo>  goodList = new ArrayList<ResponseHomeTitleInfoGoodsVo>();
		if (null != list && !list.isEmpty()) {
			List<Long> goodsIds = new ArrayList<>();
			for (GoodSerialVo gVo : list) {
				goodsIds.add(gVo.getGoodId());
			}
			Map<Long, GoodsPrice> goodsPriceMap = goodsService.getGoodsViewPriceForGoods(_store_user, goodsIds);
			Map<Long, Long> dynaByGoodsIdMap = inventoryService.findDynaByGoodsId(goodsIds);
			for (GoodSerialVo goods : list) {
				ResponseHomeTitleInfoGoodsVo vo = new ResponseHomeTitleInfoGoodsVo();
				vo.setGoodsId(goods.getGoodId());
				vo.setName(goods.getName());
				vo.setLabelName(goods.getLabelName());
				vo.setSelfSupport(goods.getSelfSupport());
				vo.setInventoryAvlNum(goods.getInventoryNum());
				vo.setNavigateImage(goods.getNavigateImage());
				vo.setOrderNo(goods.getOrderNo());
				vo.setUpOrDown(0);
				if (dynaByGoodsIdMap.containsKey(goods.getGoodId())) {
					if (null != dynaByGoodsIdMap.get(goods.getGoodId())) {
						vo.setInventoryAvlNum(dynaByGoodsIdMap.get(goods.getGoodId()));
					}else {
						vo.setInventoryAvlNum(0);
					}
				}else {
					vo.setInventoryAvlNum(0);
				}
				GoodsPrice price = null;
				if (goodsPriceMap.containsKey(goods.getGoodId())) {
					price = goodsPriceMap.get(goods.getGoodId());
					if (price != null) {
						vo.setLowestPrice(price.getSalePrice());
						vo.setMarketPirce(price.getMarketPirce());
						vo.setCommission(price.getCommission());
					}else {
						vo.setInventoryAvlNum(0);
					}
				}else {
					vo.setInventoryAvlNum(0);
				}
				String nowTime = DateUtil.currentStringDate();
				//商品标签
				List<ProductTag> ptList = new ArrayList<ProductTag>();
				//状态
				Map<String,Object> ltsMap = null;
				if (null != price) {
					ltsMap = this.goodsMapper.selectNearlyLimitTimeSale(goods.getGoodId(),price.getProductId(),nowTime);
				}
				if(ltsMap != null){
					ProductTag p = new ProductTag();
					p.setColor("#ff4b7a");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//抢购开始时间
					try {
						Date fullBeginTime = sdf.parse(ltsMap.get("fullBeginTime").toString());
						// 系统当前时间
						long nowTimeMillis = System.currentTimeMillis();
						if (fullBeginTime.getTime() > nowTimeMillis) {// 预热中
							p.setLabel("预热");
						} else {
							p.setLabel("特卖");
						}
					} catch (Exception e) {
						
					}
					ptList.add(p);
				}
				//属性
				/*if (goods.getSelfSupport() == 0) {
					ProductTag pT = new ProductTag();
					pT.setColor("#ff4b7a");
					pT.setLabel("自营");
					ptList.add(pT);
				}*/
				vo.setProductTags(ptList);
				goodList.add(vo);
			}
		}
		returnSerial.setGoodsList(goodList);
		return returnSerial;
	
	}
	

}
